﻿#region
using System;
using System.Net;
using System.Threading;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;
using MediaContent = Google.GData.YouTube.MediaContent;

#endregion

namespace Y2M.TestConsole
{
    internal class Program
    {
        private const string Key = "AI39si4QWBbUZTl_SCt_lPg_mAWrPmmyHGf_HBz3B00Ke0eGMFnz_s-rD5OMGSRMDskV3YDEvVaPK4zyzPQZd0Q3S_saB531_g";

        private const string ApplicationName = "Castpia";

        private static void Main(string[] args)
        {
            const string url = @"https://www.youtube.com/v/JY3sQVF3oDQ?version=3&f=videos&d=AQs22m0xiBq6Oq_1CszcpFMO88HsQjpE1a8d1GxQnGDm&app=youtube_gdata";
            WebClient web = new WebClient();
            web.DownloadProgressChanged += WebOnDownloadProgressChanged;
            web.DownloadFileAsync(new Uri(url), @"d:\Desktop\1.flv");
            while (true)
                Thread.Sleep(100);

            YouTubeRequestSettings settings = new YouTubeRequestSettings(ApplicationName, Key);
            YouTubeRequest request = new YouTubeRequest(settings);

            YouTubeQuery query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);

            query.OrderBy = "viewCount";
            query.Query = "희망가";
            query.SafeSearch = YouTubeQuery.SafeSearchValues.None;

            Feed<Video> videoFeed = request.Get<Video>(query);

            printVideoFeed(videoFeed);
        }

        private static void WebOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Console.WriteLine(string.Format("{0:N0} / {1:N0} / {2}", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
        }

        private static void printVideoFeed(Feed<Video> feed)
        {
            foreach (Video entry in feed.Entries)
            {
                printVideo(entry);
            }
        }

        private static void printVideo(Video video)
        {
            Console.WriteLine("Title: " + video.Title);

            foreach (MediaContent mediaContent in video.Contents)
            {
                Console.WriteLine("\tMedia Location: " + mediaContent.Url);
                Console.WriteLine("\tMedia Type: " + mediaContent.Format);
                Console.WriteLine("\tDuration: " + mediaContent.Duration);
            }
        }

        private static void printVideoEntry(Video video)
        {
            Console.WriteLine("Title: " + video.Title);
            Console.WriteLine(video.Description);
            Console.WriteLine("Keywords: " + video.Keywords);
            Console.WriteLine("Uploaded by: " + video.Uploader);
            if (video.YouTubeEntry.Location != null)
            {
                Console.WriteLine("Latitude: " + video.YouTubeEntry.Location.Latitude);
                Console.WriteLine("Longitude: " + video.YouTubeEntry.Location.Longitude);
            }
            if (video.Media != null && video.Media.Rating != null)
            {
                Console.WriteLine("Restricted in: " + video.Media.Rating.Country);
            }

            if (video.IsDraft)
            {
                Console.WriteLine("Video is not live.");
                string stateName = video.Status.Name;
                if (stateName == "processing")
                {
                    Console.WriteLine("Video is still being processed.");
                }
                else if (stateName == "rejected")
                {
                    Console.Write("Video has been rejected because: ");
                    Console.WriteLine(video.Status.Value);
                    Console.Write("For help visit: ");
                    Console.WriteLine(video.Status.Help);
                }
                else if (stateName == "failed")
                {
                    Console.Write("Video failed uploading because:");
                    Console.WriteLine(video.Status.Value);

                    Console.Write("For help visit: ");
                    Console.WriteLine(video.Status.Help);
                }

                if (video.ReadOnly == false)
                {
                    Console.WriteLine("Video is editable by the current user.");
                }

                if (video.RatingAverage != -1)
                {
                    Console.WriteLine("Average rating: " + video.RatingAverage);
                }
                if (video.ViewCount != -1)
                {
                    Console.WriteLine("View count: " + video.ViewCount);
                }

                Console.WriteLine("Thumbnails:");
                foreach (MediaThumbnail thumbnail in video.Thumbnails)
                {
                    Console.WriteLine("\tThumbnail URL: " + thumbnail.Url);
                    Console.WriteLine("\tThumbnail time index: " + thumbnail.Time);
                }

                Console.WriteLine("Media:");
                foreach (MediaContent mediaContent in video.Contents)
                {
                    Console.WriteLine("\tMedia Location: " + mediaContent.Url);
                    Console.WriteLine("\tMedia Type: " + mediaContent.Format);
                    Console.WriteLine("\tDuration: " + mediaContent.Duration);
                }
            }
        }
    }
}